﻿-- =============================================
-- Author:		<Author,XFL>
-- Edit date: <Create Date,2012 02 01>
-- Description:	<Description, [人资管理]-[员工出勤管理]-[出勤管理]
-- 部门查询是 使用了部门编号查询 BMBH >
-- =============================================
CREATE PROCEDURE [dbo].[proc_Employee_Attendance_GetList]
	(
		@StartIndex int,
		@EndIndex int,
		@CompanyId int,
		@DepartmentId int,
		@EName nvarchar(50)
	)
AS
	Begin
		

			declare @bmbh_T  varchar(50)
			set @bmbh_T = (select bmbh  FROM department WHERE id=@departmentId)		
					
		;WITH list As(Select ROW_NUMBER() OVER (ORDER BY Employee_Attendance.Id DESC)AS Row, 
			Id,
			UserName,
			EName,
			CompanyId,
			(Select c.CompanyName From Company c Where c.Id=CompanyId) as CompanyName,
			DepartmentId,
			Isnull((Select d.DName From Department d Where d.Id=DepartmentId),'---') as DepartmentName,
			TypeId,
			(Select top 1 eat.Title From Employee_Attendance_Type eat Where eat.Id=TypeId) as TypeName,
			KouKuanAmount, 
			KouKuanFromDate,
			KouKuanToDate,
			Description,
			--(Select top 1 e.EName From Employee e Where e.UserName=CreateName) as CreateName,
			Isnull((Select top 1 e.EName From Employee e Where e.UserName=CreateName),CreateName) as CreateName,
			CreateDate,
			Wage_FF,
			t_TypeName,isnull([days] ,0) [days],isnull([minute],0) as [minute]
		From
			Employee_Attendance
		Where 
			 CompanyId = @CompanyId
			 and (@DepartmentId = 0 or DepartmentId in(Select Id From department Where Left(bmbh,Len(@bmbh_T))=@bmbh_T))
			 and EName Like '%'+@EName+'%'			    
		)
		Select *,(select count(0) from list)  AS Count_Sum From list Where Row between @StartIndex and @EndIndex 	order by row					    
	End
	RETURN
